//index.js
//获取应用实例
const app = getApp()
var sliderWidth = 48; // 需要设置slider的宽度，用于计算中间位置
var rq = require("../../utils/https.js")
Page({
  data: {

    //首页基础功能
    tabs: ["分级地址", "SN码", "分光器", "固话/宽带", "姓名", "模糊地址"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    system_info: {}, //系统信息
    panel_height: 0,
    back_up: [false, false, false, false, false, false], //是否允许返回上一级（默认全部都不允许）

    //分级地址查询
    tab1_level_array: [],
    tab1_keywords: ['河北省', '唐山', '曹妃甸区'],
    tab1_level_table_show: false,
    tab1_level_table: [],
    level_detail: [],
    tab1_detail1: false,
    tab1_detail2: false,
    tab1_detail2_data: {},
    //SN码查询
    sn_showed: false,
    sn_val: "",
    sn_search_result: [],
    tab2_detail_data: {},
    //分光器查询（obd）
    obd_showed: false,
    obd_val: "",
    obd_search_result: [],
    obd_detail: [],
    tab3_detail_data: {},
    tab3_step: 1,
    //固话/宽带
    tel_showed: false,
    tel_val: "",
    tel_search_result: [],
    tab4_detail_data: {},
    //姓名
    name_showed: false,
    name_val: "",
    name_search_result: [],
    tab5_detail_data: {},
    //模糊地址查询
    address_showed: false,
    address_val: "",
    address_search_result: [],
    address_detail: [],
    tab6_detail_data: {},
    tab6_step: 1,
  },
  onLoad: function() {
    var that = this;
    //屏幕高度
    let system_info = wx.getSystemInfoSync();
    let panel_height = system_info.windowHeight - 80
    that.setData({
      system_info: system_info,
      panel_height: panel_height
    })
    //tab滚动条
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
    that.loadLevel();
  },
  // tab切换
  tabClick: function(e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },
  //加载分级
  loadLevel: function(tab1_keywords = ['河北省', '唐山', '曹妃甸区']) {
    let that = this
    let back_up = that.data.back_up
    if (tab1_keywords.length <= 3) {
      //分级地址查询第一级
      rq.request('gpon/level', {}, function(result) {
        if (result.code == 0) {
          //查询第一级同时禁止使用返回上一级
          back_up[0] = false
          that.setData({
            tab1_level_array: [result.data.result],
            back_up: back_up
          })
        } else {
          wx.showToast({
            title: '分级地址筛选加载失败',
            icon: 'none'
          })
        }
      })
    } else {
      //非第一级
      rq.request('gpon/level', {
        keywords: tab1_keywords
      }, function(result) {
        if (result.code == 0) {
          let tab1_level_array = that.data.tab1_level_array
          tab1_level_array.push(result.data.result)
          back_up[0] = true
          that.setData({
            tab1_level_array: tab1_level_array,
            back_up: back_up,
          })
          if (result.data.fenguangqi) {
            that.setData({
              tab1_level_table: result.data.fenguangqi,
              tab1_level_table_show: true
            })
          } else {
            that.setData({
              tab1_level_table_show: false
            })
          }
        }
      })
    }
  },
  //点击返回上一级
  bindBackupTap() {
    let that = this
    let activeIndex = this.data.activeIndex
    let back_up = this.data.back_up
    if (!back_up[activeIndex]) {
      wx.showToast({
        title: '已经到最上层了',
        icon: 'none'
      })
      return
    }
    //如果没有达到最上层，允许向上返回
    switch (parseInt(activeIndex)) {
      case 0:
        //判断是否正在查看详情，返回详情页面
        if (that.data.tab1_detail1) {
          if (that.data.tab1_detail2) {
            that.setData({
              tab1_detail2: false,
              tab1_level_table_show: true,
            })
          } else {
            that.setData({
              tab1_detail1: false
            })
          }
          return
        }
        //分级地址查询，向上返回的时候pop最后一层,当前层数减一
        let tab1_level_array = that.data.tab1_level_array;
        let tab1_keywords = that.data.tab1_keywords;
        tab1_level_array.pop();
        tab1_keywords.pop();
        if (tab1_keywords.length < 4) {
          back_up[0] = false
        }
        that.setData({
          tab1_level_array: tab1_level_array,
          tab1_keywords: tab1_keywords,
          back_up: back_up,
          tab1_level_table_show: false
        })
        break;
      case 1:
        back_up[1] = false
        that.setData({
          back_up: back_up
        })
        break;
      case 2:
        let tab3_step = that.data.tab3_step
        if (tab3_step == 2) {
          back_up[2] = false
        }
        if (tab3_step > 1) {
          that.setData({
            tab3_step: --tab3_step,
            back_up: back_up
          })
        }
        break;
      case 3:
        back_up[3] = false
        that.setData({
          back_up: back_up
        })
        break;
      case 4:
        back_up[4] = false
        that.setData({
          back_up: back_up
        })
        break;
      case 5:
        let tab6_step = that.data.tab6_step
        if (tab6_step == 2) {
          back_up[5] = false
        }
        if (tab6_step > 1) {
          that.setData({
            tab6_step: --tab6_step,
            back_up: back_up
          })
        }
        break;
    }
  },
  //加载下一级
  bindLevelNextTap(e) {
    let that = this
    let tab1_keywords = this.data.tab1_keywords;
    tab1_keywords.push(e.currentTarget.id)
    this.setData({
      tab1_keywords: tab1_keywords
    }, function() {
      that.loadLevel(tab1_keywords)
    })
  },
  //分级地址查询点击进入详情
  bindLevelDetailTap(e) {
    let that = this
    console.log(e.currentTarget.id)
    rq.request('gpon/level/detail', {
      obd_id: e.currentTarget.id
    }, function(result) {
      if (result.code == 0) {
        that.setData({
          level_detail: result.data,
          tab1_detail1: true
        })
      } else {
        wx.showToast({
          title: '加载失败请重试',
          icon: 'none'
        })
      }

    })
  },
  bindLevelDetailTap2(e) {
    let that = this;
    let id = e.currentTarget.id;
    let index = parseInt(id) - 1;
    if (that.data.level_detail[index]) {
      that.setData({
        tab1_detail2: true,
        tab1_level_table_show: false,
        tab1_detail2_data: that.data.level_detail[index]
      })
    }
  },
  //SN码查询搜索框功能
  showSNInput: function() {
    this.setData({
      sn_showed: true
    });
  },
  hideSNInput: function() {
    this.setData({
      sn_val: "",
      sn_showed: false
    });
  },
  clearSNInput: function() {
    this.setData({
      sn_val: ""
    });
  },
  inputSNTyping: function(e) {
    let that = this
    this.setData({
      sn_val: e.detail.value
    });
    rq.request('gpon/sn', {
      sn_code: e.detail.value
    }, function(result) {
      if (result.code == 0) {
        that.setData({
          sn_search_result: result.data
        })
      } else {
        wx.showToast({
          title: result.msg,
          icon: 'none'
        })
      }
    })
  },
  bindSNDetailTap: function(e) {
    let that = this
    wx.showLoading({
      title: '正在加载',
    })
    rq.request('gpon/sn/detail', {
      sn_code: e.currentTarget.id
    }, function(result) {

      if (result.code == 0) {
        let back_up = that.data.back_up
        back_up[1] = true
        that.setData({
          tab2_detail_data: result.data,
          back_up: back_up,
        }, function() {
          wx.hideLoading()
        })
      } else {
        wx.hideLoading()
        wx.showToast({
          title: result.msg,
          icon: 'none'
        })
      }
    })
  },
  //分光器查询搜索框功能
  showOBDInput: function() {
    this.setData({
      obd_showed: true
    });
  },
  hideOBDInput: function() {
    this.setData({
      obd_val: "",
      obd_showed: false
    });
  },
  clearOBDInput: function() {
    this.setData({
      obd_val: ""
    });
  },
  inputOBDTyping: function(e) {
    let that = this
    this.setData({
      obd_val: e.detail.value
    });
    rq.request('gpon/obd', {
      obd_name: e.detail.value
    }, function(result) {
      if (result.code == 0) {
        that.setData({
          obd_search_result: result.data
        })
      } else {
        wx.showToast({
          title: result.msg,
          icon: 'none'
        })
      }
    })
  },
  //第一次点击
  bindOBDDetailTap: function(e) {
    let that = this
    let back_up = that.data.back_up;
    rq.request('gpon/level/detail', {
      obd_id: e.currentTarget.id
    }, function(result) {
      if (result.code == 0) {
        back_up[2] = true
        that.setData({
          obd_detail: result.data,
          tab3_step: 2,
          back_up: back_up,
        })
      } else {
        wx.showToast({
          title: '加载失败请重试',
          icon: 'none'
        })
      }

    })

  },
  //第二次点击
  bindOBDDetailTap2(e) {
    let that = this;
    let id = e.currentTarget.id;
    let index = parseInt(id) - 1;
    let back_up = that.data.back_up;
    if (that.data.obd_detail[index]) {
      back_up[2] = true;
      that.setData({
        tab3_step: 3,
        tab3_detail_data: that.data.obd_detail[index],
        back_up: back_up
      })
    }
  },
  //固话/宽带搜索
  showTELInput: function() {
    this.setData({
      tel_showed: true
    });
  },
  hideTELInput: function() {
    this.setData({
      tel_val: "",
      tel_showed: false
    });
  },
  clearTELInput: function() {
    this.setData({
      tel_val: ""
    });
  },
  inputTELTyping: function(e) {
    let that = this
    this.setData({
      tel_val: e.detail.value
    });
    rq.request('gpon/tel', {
      tel: e.detail.value
    }, function(result) {
      if (result.code == 0) {
        that.setData({
          tel_search_result: result.data
        })
      } else {
        wx.showToast({
          title: result.msg,
          icon: 'none'
        })
      }
    })
  },
  bindTELDetailTap: function(e) {
    let that = this
    wx.showLoading({
      title: '正在加载',
    })
    rq.request('gpon/tel/detail', {
      tel: e.currentTarget.id
    }, function(result) {

      if (result.code == 0) {
        let back_up = that.data.back_up
        back_up[3] = true
        that.setData({
          tab4_detail_data: result.data,
          back_up: back_up,
        }, function() {
          wx.hideLoading()
        })
      } else {
        wx.hideLoading()
        wx.showToast({
          title: result.msg,
          icon: 'none'
        })
      }
    })
  },


  //姓名搜索
  showNameInput: function() {
    this.setData({
      name_showed: true
    });
  },
  hideNameInput: function() {
    this.setData({
      name_val: "",
      name_showed: false
    });
  },
  clearNameInput: function() {
    this.setData({
      name_val: ""
    });
  },
  inputNameTyping: function(e) {
    let that = this
    this.setData({
      name_val: e.detail.value
    });
    rq.request('gpon/name', {
      name: e.detail.value
    }, function(result) {
      if (result.code == 0) {
        that.setData({
          name_search_result: result.data
        })
      } else {
        wx.showToast({
          title: result.msg,
          icon: 'none'
        })
      }
    })
  },
  bindNameDetailTap: function(e) {
    let that = this
    wx.showLoading({
      title: '正在加载',
    })
    let item = e.currentTarget.dataset
    rq.request('gpon/name/detail', {
      name: item.user_name,
      obd_name: item.obd_name,
      obd_terminal: item.obd_terminal
    }, function(result) {

      if (result.code == 0) {
        let back_up = that.data.back_up
        back_up[4] = true
        that.setData({
          tab5_detail_data: result.data,
          back_up: back_up,
        }, function() {
          wx.hideLoading()
        })
      } else {
        wx.hideLoading()
        wx.showToast({
          title: result.msg,
          icon: 'none'
        })
      }
    })
  },


  //模糊查询搜索框功能
  showAddressInput: function() {
    this.setData({
      address_showed: true
    });
  },
  hideAddressInput: function() {
    this.setData({
      address_val: "",
      address_showed: false
    });
  },
  clearAddressInput: function() {
    this.setData({
      address_val: ""
    });
  },
  inputAddressTyping: function(e) {
    let that = this
    this.setData({
      address_val: e.detail.value
    });
    rq.request('gpon/address', {
      address: e.detail.value
    }, function(result) {
      if (result.code == 0) {
        that.setData({
          address_search_result: result.data
        })
      } else {
        wx.showToast({
          title: result.msg,
          icon: 'none'
        })
      }
    })
  },
  //第一次点击
  bindAddressDetailTap: function(e) {
    let that = this
    let back_up = that.data.back_up;
    rq.request('gpon/level/detail', {
      obd_id: e.currentTarget.id
    }, function(result) {
      if (result.code == 0) {
        back_up[5] = true
        that.setData({
          address_detail: result.data,
          tab6_step: 2,
          back_up: back_up,
        })
      } else {
        wx.showToast({
          title: '加载失败请重试',
          icon: 'none'
        })
      }

    })

  },
  //第二次点击
  bindAddressDetailTap2(e) {
    let that = this;
    let id = e.currentTarget.id;
    let index = parseInt(id) - 1;
    let back_up = that.data.back_up;
    if (that.data.address_detail[index]) {
      back_up[5] = true;
      that.setData({
        tab6_step: 3,
        tab6_detail_data: that.data.address_detail[index],
        back_up: back_up
      })
    }
  },



});